package com.szqhyxj.youxiangju.biz.creditReport;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.creditCardEvaluation.CreditCardEcaluationActivity;
import com.szqhyxj.youxiangju.biz.creditReport.signature.SignatureActivity;
import com.szqhyxj.youxiangju.biz.push.AddDeviceEvent;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by leung on 2018/11/4.
 */

public class CreditReportActivity extends BaseActivity implements CreditReportContract.View, NavigationBar.NavigationBarListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.debitard)
    ImageView debitard;
    CreditReportPresenter presenter;
    CreditReportEntity creditReportEntity;

    @BindView(R.id.commit_creditReport)
    Button commitCreditReport;

    @BindView(R.id.credittiparea)
    TextView credittiparea;
    @BindView(R.id.stepicon)
    ImageView stepicon;
    @BindView(R.id.step)
    TextView step;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.timeArea)
    LinearLayout timeArea;
    private int STARTREPORT = 1;
    private int REPORTING = 2;
    private int FINISHREPORT = 3;
    private int FAILREPORT = 4;
    static String CardId;
    private int outFlag = 0;

    public static void start(Activity activity, String cardid) {
        Intent intent = new Intent();
        intent.setClass(activity, CreditReportActivity.class);
        activity.startActivity(intent);
        CardId = cardid;

    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_report);
        ButterKnife.bind(this);
        presenter = new CreditReportPresenter(this);
        presenter.init();
        presenter.getCardInfo(CardId);
        navigationBar.setNavigationBarListener(this);
        //注册事件
      //  EventBus.getDefault().register(this);
        timeArea.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解除注册
     //   EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            creditReportEntity = data.getParcelableExtra("result");
            showCommit(REPORTING);
            showelectronicsignature();

        }
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
        }


    }


    @Override
    public void showCard(String s) {
        Glide.with(this).load(s).into(debitard);
    }


    @Override
    public void showInfo(CreditReportEntity creditReportEntity) {
        this.creditReportEntity = creditReportEntity;
    }

    @Override
    public void showToast(String s) {

        Toast.makeText(this, s + "请勿重复点击", Toast.LENGTH_LONG).show();


    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void acceptActionEvent(AddDeviceEvent addDeviceEvent) {
        this.dismissLoading();
        if (addDeviceEvent.isIsaddWork()) {
            presenter.setshowconsumption();
            credittiparea.setVisibility(View.GONE);


        } else {
            presenter.setenshowconsumption();
            credittiparea.setVisibility(View.VISIBLE);
            credittiparea.setText(addDeviceEvent.getMsg());
        }
    }


    @Override
    public void showelectronicsignature() {

        stepicon.setImageDrawable(getResources().getDrawable(R.mipmap.shurumima));

        step.setText("请在设备输入卡交易密码");

    }

    @Override
    public void showconsumption() {
        stepicon.setImageDrawable(getResources().getDrawable(R.mipmap.baobeiwancheng));
        step.setText("消费成功，报备已完成");
        commitCreditReport.setEnabled(true);
        presenter.setReportFinsh();
    }

    @Override
    public void showenconsumption() {
        stepicon.setImageDrawable(getResources().getDrawable(R.mipmap.baobeishibai));
        step.setText("消费失败，请重新报备");
        step.setTextColor(getResources().getColor(R.color.textcolor4e));
        commitCreditReport.setEnabled(true);
        presenter.setReportFail();
        timeArea.setVisibility(View.GONE);
    }



    public void showreportFail() {
        step.setTextColor(getResources().getColor(R.color.black));
        step.setText("请在设备输入卡交易密码");
        stepicon.setImageDrawable(getResources().getDrawable(R.mipmap.shurumima));
    }

    private int flag;

    @Override
    public void showCommit(int i) {
        if (i == STARTREPORT) {
            timeArea.setVisibility(View.GONE);
            commitCreditReport.setText("确认已插卡");
            flag = STARTREPORT;
            outFlag = 0;
        }
        if (i == REPORTING) {
            timeArea.setVisibility(View.VISIBLE);
            presenter.setTime();
            commitCreditReport.setText("正在报备中...");
            flag = REPORTING;
            outFlag = 1;
        }
        if (i == FINISHREPORT) {
            timeArea.setVisibility(View.GONE);
            commitCreditReport.setText("报备成功");
            flag = FINISHREPORT;
            outFlag = 0;
        }
        if (i == FAILREPORT) {
            presenter.clean();
            time.setText("200");
            timeArea.setVisibility(View.GONE);
            commitCreditReport.setText("重新报备");
            flag = FAILREPORT;
            outFlag = 0;



        }
    }


    @Override
    public void notoCommit() {
        commitCreditReport.setEnabled(false);
        commitCreditReport.setText("正在报备中...");

    }

    @Override
    public void showTime(String min) {
        time.setText(min + "");
    }

    @Override
    public void showOut() {
        outFlag = 0;

        timeArea.setVisibility(View.GONE);
        credittiparea.setVisibility(View.GONE);

    }


    @OnClick(R.id.commit_creditReport)
    public void onClick() {
        if (flag == STARTREPORT) {
            SignatureActivity.start(this, creditReportEntity);
        }
        if (flag == REPORTING) {
            commitCreditReport.setEnabled(false);
        }
        if (flag == FINISHREPORT) {
            commitCreditReport.setEnabled(true);
            finish();
        }
        if (flag == FAILREPORT) {
            showreportFail();
            commitCreditReport.setEnabled(true);
            presenter.commit(creditReportEntity);
            timeArea.setVisibility(View.VISIBLE);
            outFlag = 1;

        }

    }

    @Override
    public void navigationLeft() {
        if (outFlag == 1) {
            Toast.makeText(this, "正在报备，请勿退出！", Toast.LENGTH_LONG).show();
            return;
        } else {
            finish();
        }

    }

    @Override
    public void navigationRight() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Boolean key = false;
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (outFlag == 1) {
                Toast.makeText(this, "正在报备，请勿退出！", Toast.LENGTH_LONG).show();
                key = true;

            } else {
                finish();
                key = false;
            }
        }
        return key;
    }

}
